package com.ysd.lis.service.bac.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.BacStrainLog;
import com.ysd.lis.mapper.bac.BacStrainLogMapper;
import com.ysd.lis.service.bac.BacStrainLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 细菌过程记录 服务实现类
 * </p>
 *
 * @author wanghao
 * @since 2024-04-13
 */
@Service
public class BacStrainLogServiceImpl extends ServiceImpl<BacStrainLogMapper, BacStrainLog> implements BacStrainLogService {

    @Autowired
    BacStrainLogMapper bacStrainLogMapper;

    @Override
    public Result findStrainLogList(BacStrainLog bacStrainLog) {
        MPJLambdaWrapper<BacStrainLog> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(BacStrainLog.class);
        queryWrapper.selectAs("oprt", SysUser::getRealname, BacStrainLog::getOperatorNa);
        queryWrapper.eq(BacStrainLog::getDelFlag, 0);
        //queryWrapper.eq(BacStrainLog::getMaininfoId, bacStrainLog.getMaininfoId());
        queryWrapper.eq(BacStrainLog::getRptGroup, bacStrainLog.getRptGroup());
        //queryWrapper.eq(BacStrainLog::getSampleda, bacStrainLog.getSampleda());
        if (BeanUtil.isNotEmpty(bacStrainLog.getSampleda())) {
            Timestamp timestamp = new Timestamp(bacStrainLog.getSampleda().getTime());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = sdf.format(timestamp);
            Timestamp startTime = Timestamp.valueOf(formattedDate + " 00:00:00");
            Timestamp endTime = Timestamp.valueOf(formattedDate + " 23:59:59");
            queryWrapper.ge(BacStrainLog::getSampleda, startTime);
            queryWrapper.le(BacStrainLog::getSampleda, endTime);
        }
        queryWrapper.eq(BacStrainLog::getSampleNo, bacStrainLog.getSampleNo());

        //queryWrapper.eq(BacStrainLog::getWkFlwId, bacStrainLog.getWkFlwId());
        queryWrapper.eq(BacStrainLog::getTestFlowNo, bacStrainLog.getTestFlowNo());

        //queryWrapper.eq(BacStrainLog::getWkFlwNodeId, bacStrainLog.getWkFlwNodeId());
        queryWrapper.eq(BacStrainLog::getTestNodeNo, bacStrainLog.getTestNodeNo());

        //queryWrapper.eq(StringUtils.isNotBlank(bacStrainLog.getStrainId()), BacStrainLog::getStrainId, bacStrainLog.getStrainId());
        if (StringUtils.isNotBlank(bacStrainLog.getStrainNo())) {
            queryWrapper.eq(BacStrainLog::getStrainNo, bacStrainLog.getStrainNo());
        }
        queryWrapper.eq(StringUtils.isNotBlank(bacStrainLog.getCulMediumNo()), BacStrainLog::getCulMediumNo, bacStrainLog.getCulMediumNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacStrainLog.getCulMediumNo()), BacStrainLog::getCulMediumNo, bacStrainLog.getCulMediumNo());
        queryWrapper.eq(StringUtils.isNotBlank(bacStrainLog.getCulCombinNo()), BacStrainLog::getCulCombinNo, bacStrainLog.getCulCombinNo());
        queryWrapper.leftJoin(SysUser.class, "oprt", p -> p.eq(SysUser::getUsername, BacStrainLog::getOperatorDoctor).eq(BacStrainLog::getDelFlag, 0).isNotNull(SysUser::getUsername).ne(SysUser::getUsername, ""));
        queryWrapper.orderByDesc(BacStrainLog::getCreateTime);
        List<BacStrainLog> bacStrainLogs = bacStrainLogMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", bacStrainLogs);
    }
}
